import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import '../provide/counter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

//class ShopCarPage extends StatefulWidget {
//  @override
//  _ShopCarPageState createState() => _ShopCarPageState();
//}
//
//class _ShopCarPageState extends State<ShopCarPage> {
//  @override
//  Widget build(BuildContext context) {
//    return Scaffold(
//      appBar: AppBar(title: Text('购物车'),
//      ),
//      body: Center(
//         child: Column(
//            children: <Widget>[
//             Number(),
//              MyButton()
//            ],
//         ),
//      ),
//    );
//  }
//}
//
//
//class Number extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    return Container(
//      child: Provide<Counter>(
//        builder: (context,child,counter){
//          return Text('${counter.value}',
//            style: Theme.of(context).textTheme.display1,
//          );
//        },
//      ),
//    );
//  }
//}
//class MyButton extends StatelessWidget {
//  @override
//  Widget build(BuildContext context) {
//    return RaisedButton(onPressed: (){
//      Provide.value<Counter>(context).increment();
//    },
//      padding: EdgeInsets.only(top: 10),
//      child: Text('递增'),
//    );
//  }
//}


class ShopCarPage extends StatefulWidget {
  @override
  _ShopCarPageState createState() => _ShopCarPageState();
}

class _ShopCarPageState extends State<ShopCarPage> {

  List<String> testList = [];
  @override
  Widget build(BuildContext context) {
//    _show();
    return Scaffold(
      appBar: AppBar(title: Text('购物车'),),
      body: Stack(
          children: <Widget>[
            ListView.builder(
              itemCount: 10,
              itemBuilder: (context,index){
                return _shopCarItem();
              }
            ),
//            Container(
//              child: Column(
//                children: <Widget>[
//                  _shopCarItem(),
//                  _shopCarItem(),
//                  _shopCarItem(),
//                  _shopCarItem(),
////                  _cartBottomBar()
////
////            Container(
////              height: 500,
////              child: ListView.builder(
////                itemCount: testList.length,
////                itemBuilder: (context, index){
////                  return ListTile(
////                    title: Text(testList[index]),
////                  );
////                },
////              ),
////            ),
////            RaisedButton(
////              onPressed: (){
////                _add();
////              },
////              child: Text('增加'),
////            ),
////            RaisedButton(
////              onPressed: (){
////                _clear();
////              },
////              child: Text('清空'),
////            )
//                ],
//              ),
//            ),
            Positioned(
                  bottom: 0,
                  left: 0,
                  child:_cartBottomBar()
              )
          ],
        ),
    );
  }

  //增加方法
  void _add() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String temp = '数据的持久化';
    testList.add(temp);
    prefs.setStringList('testInfo', testList);
    _show();
  }

  //查询方法
  void _show() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if(prefs.getStringList('testInfo') != null) {
      setState(() {
        testList = prefs.getStringList('testInfo');
      });
    }
  }
  //删除方法
  void _clear() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
//    prefs.clear();
//    if(prefs.getStringList('testInfo') != null) {
//      setState(() {
//        testList = prefs.getStringList('testInfo');
//      });
//    }
    prefs.remove('testInfo');
    setState(() {
      testList = [];
    });
  }
  Widget _shopCarItem(){
    return Container(
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(200),
      margin: EdgeInsets.fromLTRB(5, 1, 5, 2),
      padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(width: 1,color: Colors.black12),
        )
      ),
      child: Row(
        children: <Widget>[
          _cartCheckButton(1),
          _cartImage(),
          _cartGoodsName(1),
          _cartPrice()
        ],
      ),
    );
  }
  //多选按钮
  Widget _cartCheckButton(item) {
    return Container(
      child: Checkbox(
        value: true,
        activeColor: Colors.pink,
        onChanged: (bool value){

        },
      ),
    );
  }
  Widget _cartImage(){
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(3),
      decoration: BoxDecoration(
        border: Border.all(
          color: Colors.black12,
          width: 1
        )
      ),
      child: Image.network('http://images.baixingliangfan.cn/firstCategoryPicture/20190225/20190225232703_9950.png'),
    );
  }
  //商品名称
  Widget _cartGoodsName(item) {
    return Container(
      width: ScreenUtil().setWidth(300),
      padding: EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: <Widget>[
          Text('商品名称'),
          _cartCount()
        ],
      ),
    );
  }
  Widget _cartCount(){
    return Container(
      width: ScreenUtil().setWidth(165),
      margin: EdgeInsets.only(top: 5),
      decoration: BoxDecoration(
        border: Border.all(width: 1,color: Colors.black12)
      ),
      child: Row(
        children: <Widget>[
          InkWell(
            onTap: (){},
            child: Container(
              width: ScreenUtil().setWidth(45),
              height: ScreenUtil().setHeight(45),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                  right: BorderSide(width: 1,color: Colors.black12)
                )
              ),
              child: Text('-'),
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(70),
            height: ScreenUtil().setHeight(45),
            alignment: Alignment.center,
            color: Colors.white,
            child: Text('1'),
          ),
          InkWell(
            onTap: (){},
            child: Container(
              width: ScreenUtil().setWidth(45),
              height: ScreenUtil().setHeight(45),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border(
                      left: BorderSide(width: 1,color: Colors.black12)
                  )
              ),
              child: Text('+'),
            ),
          )
        ],
      ),
    );
  }
  //商品价格
  Widget _cartPrice(){
    return Container(
      width: ScreenUtil().setWidth(150),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Text('￥${300}'),
          Text('￥${350}',
            style: TextStyle(
              color: Colors.black12,
              decoration: TextDecoration.lineThrough,
              decorationColor: Colors.black12
            ),
          ),
          Container(
            child: InkWell(
              onTap: (){},
              child: Icon(Icons.delete_forever,color: Colors.black26,),
            ),
          )
        ],
      ),

    );
  }
  
  //底部结算栏 
  Widget _cartBottomBar(){
    return Container(
      width: ScreenUtil().setWidth(750),
      padding: EdgeInsets.all(5),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Row(
            children: <Widget>[
              Checkbox(value: true, onChanged: (bool value){},activeColor: Colors.pink,),
              Text('全选',textAlign: TextAlign.center,style: TextStyle(fontSize: 20,color: Colors.black),),
            ],
          ),
          Container(
            width: ScreenUtil().setWidth(430),
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(
                      width: ScreenUtil().setWidth(280),
                      alignment: Alignment.centerRight,
                      child:  Text('合计:',style: TextStyle(fontSize: ScreenUtil().setSp(36)),),
                    ),
                    Container(
                      width: ScreenUtil().setWidth(150),
                      alignment: Alignment.centerLeft,
                      child: Text('￥${1992.00}',style: TextStyle(color: Colors.pink,fontSize: ScreenUtil().setSp(36)),),
                    )
                  ],
                ),
                Container(
                 width: ScreenUtil().setWidth(430),
                  alignment: Alignment.centerRight,
                  child: Text('满10元免配送费，预约免配送费',style: TextStyle(
                    color: Colors.black38,
                    fontSize: ScreenUtil().setSp(22)
                  ),),
                )
              ],
            ),
          ),
          Container(
          width: ScreenUtil().setWidth(140),
          padding: EdgeInsets.only(left: 10),
          child: InkWell(
            onTap: (){},
            child: Container(
              padding: EdgeInsets.all(10),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(3)
              ),
              child: Text('结算',style: TextStyle(color: Colors.white),),
            ),
          ),
          ),
        ],
      ),
    );
  }
}




